﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Configuration;
using MySql.Data.MySqlClient;

namespace Common.SQL
{
    /// <summary>
    /// 数据库连接池类(MySQL)
    /// 作者：wgy
    /// 日期：2011.07.09
    /// </summary>
    public class DBPool
    {
        private static readonly string MySqlConnString = ConfigurationManager.ConnectionStrings["MySQLConnString1"].ConnectionString;

        log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private static DBPool _instance = null;

        public static DBPool Instance()
        {
            if (_instance == null)
                _instance = new DBPool();
            return _instance;
        }

        /// <summary>
        /// 获得一个数据库连接
        /// </summary>
        /// <returns></returns>
        public IDbConnection GetConnection()
        {
            MySqlConnection conn = new MySqlConnection(MySqlConnString);

            logger.Debug("获得连接[" + conn.GetHashCode() + "]");
            
            return conn;
        }

        /// <summary>
        /// 释放一个数据库连接
        /// </summary>
        /// <param name="conn"></param>
        public void FreeConnection(IDbConnection conn)
        {
            logger.Debug("释放连接[" + conn.GetHashCode() + "]");

            if (conn != null)
                conn.Close();
        }
    }
}
